<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="mini" label-width="68px">
          <el-form-item label="企业代码" prop="coCode">
            <el-input
              v-model.trim="queryParams.coCode"
              placeholder="请输入企业代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="主管海关" prop="mainPort">
            <el-select v-model.trim="queryParams.mainPort" placeholder="请选择主管海关" filterable clearable>
              <el-option v-for="(item, index) in customsList" :key="index" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="场所编码" prop="fieldCode">
            <el-select v-model.trim="queryParams.fieldCode" placeholder="请选择场所编码" filterable clearable>
              <el-option
                v-for="dict in fieldOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="中文名称" prop="coNameCn">
            <el-input
              v-model.trim="queryParams.coNameCn"
              placeholder="请输入企业中文名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="外文名称" prop="coNameEn">
            <el-input
              v-model.trim="queryParams.coNameEn"
              placeholder="请输入企业外文名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="经营类别" prop="coDealType">
            <el-select v-model.trim="queryParams.coDealType" placeholder="请选择企业经营类别" clearable>
              <el-option v-for="(item, index) in dict.type.co_deal_type" :key="index" :label="item.value+'-'+item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="备案类别" prop="coRecType">
            <el-select v-model.trim="queryParams.coRecType" placeholder="请选择企业备案类别" clearable>
              <el-option v-for="(item, index) in dict.type.co_rec_type" :key="index" :label="item.value+'-'+item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="申报状态" prop="proposeStatus">
            <el-select v-model.trim="queryParams.proposeStatus" placeholder="请选择申报状态" clearable>
              <el-option v-for="(item, index) in dict.type.sw_vehicle_decl_status" :key="index"
                         :label="item.value+'-'+item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                            range-separator="-" start-placeholder="开始日期"
                            end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddEdit('add')"
            v-hasPermi="['bt:corporationInfo:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['bt:corporationInfo:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleDecl"
            v-hasPermi="['bt:corporationInfo:decl']"
          >申报
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-upload"
            size="mini"
            :disabled="single"
            @click="handleAddEdit('declModify')"
            v-hasPermi="['bt:corporationInfo:editDecl']"
          >变更申报
          </el-button>
        </el-col>
        <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['corporation:corporationInfo:export']"
            >导出</el-button>
          </el-col>
          -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <basic-table
        ref="tableList"
        :columns="columns"
        :tableData="tableData"
        :total="pageParams.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        :loading="loading"
        :needCheckbox="true"
        @selectionChange="handleSelectionChange"
      >
        <template #proposeStatus="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_decl_status"
            :value="scope.row.proposeStatus"
          ></dict-tag>
        </template>
        <template #coDealType="scope">
          <dict-tag
            :options="dict.type.co_deal_type"
            :value="scope.row.coDealType"
          ></dict-tag>
        </template>
        <template #assureType="scope">
          <dict-tag
            :options="dict.type.sw_assure_type"
            :value="scope.row.assureType"
          ></dict-tag>
        </template>
        <template #coRecType="scope">
          <dict-tag
            :options="dict.type.co_rec_type"
            :value="scope.row.coRecType"
          ></dict-tag>
        </template>
        <template #responseCode="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_rcpt_status"
            :value="scope.row.responseCode"
          ></dict-tag>
        </template>
        <template #operate="scope">
          <div class="option-menu">
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleAddEdit('edit', scope.row)"
              v-hasPermi="['bt:corporationInfo:edit']"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDelete(scope.row)"
              v-hasPermi="['bt:corporationInfo:remove']"
            >
              删除
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </div>
        </template>
      </basic-table>
    </basic-container>

    <!-- 新增、编辑 -->
    <add-edit
      ref="addEdit"
      :dialogType="dialogType"
      :rowData="rowData"
      :dict="dictProp"
      :customsList="customsList"
      :fieldOptions="fieldOptions"
      @handleQuery="handleQuery"
    />

    <!-- 详情 -->
    <detail-dialog
      ref="detail"
      :customsList="customsList"
      :detailId="detailId"
      :dict="dictProp"
    ></detail-dialog>

  </div>
</template>

<script>
import {
  listCorporationInfo,
  delCorporationInfo,
  exportCorporationInfo,
  declCorporationInfo
} from "@/api/swdecl/corporationInfo";
import addEdit from "./components/addEdit";
import detailDialog from "./components/detailDialog";
import {getCustomsList} from "@/api/bt/bmhs";
import {findZcField} from "@/api/system/baseFieldInfo";
import {declRoadTrailer} from "@/api/swdecl/roadTrailer";

export default {
  name: "CorporationInfo",
  components: {
    addEdit,
    detailDialog
  },
  dicts: ["sw_vehicle_decl_status", "sw_vehicle_rcpt_status", "co_rec_type", "co_deal_type", "sw_assure_type", "sw_vehicle_reg_place"],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coCode: null,
        mainPort: null,
        coNameCn: null,
        coNameEn: null,
        coRecType: null,
        fieldCode: null,
        proposeStatus: null,
        responseType: null,
      },
      // 遮罩层
      loading: true,
      dateRange:[],
      // 列表数据
      tableData: [],
      columns: [
        {
          prop: "coCode",
          label: "企业代码",
          align: "center",
          minWidth: 130
        },
        {
          prop: "coNameCn",
          label: "企业中文名称",
          align: "center",
          minWidth: 110
        },
        {
          prop: "mainPort",
          label: "主管海关",
          align: "center"
        },
        {
          prop: "fieldCode",
          label: "场所编码",
          align: "center"
        },
        {
          prop: "coNameEn",
          label: "企业外文名称",
          align: "center",
          minWidth: 110
        },
        {
          prop: "coDealType",
          label: "企业经营类型",
          align: "center",
          minWidth: 110,
          slot: true
        },
        {
          prop: "coRecType",
          label: "企业备案类别",
          align: "center",
          minWidth: 180,
          slot: true
        },
        {
          prop: "apprNo",
          label: "许可批文号",
          align: "center",
          minWidth: 110
        },
        {
          prop: "apprValidTime",
          label: "批文有效期",
          align: "center",
          minWidth: 100
        },
        {
          prop: "coRep",
          label: "企业负责人",
          align: "center",
          minWidth: 100
        },
        {
          prop: "coRepPhone",
          label: "联系电话",
          align: "center",
          minWidth: 110
        },
        {
          prop: "assureType",
          label: "担保方式",
          align: "center",
          slot: true,
          minWidth: 80
        },
        {
          prop: "assureLetterNo",
          label: "担保函编号",
          align: "center",
          minWidth: 110
        },
        {
          prop: "proposeStatus",
          label: "申报状态",
          align: "center",
          minWidth: 110,
          slot: true,
          fixed: "right"
        },
        {
          prop: "createBy",
          label: "创建人",
          align: "center"
        },
        {
          prop: "createTime",
          label: "创建时间",
          align: "center",
          width: 140,
        },
        {
          prop: "responseCode",
          label: "回执状态",
          align: "center",
          width: 110,
          slot: true,
          fixed: "right"
        },
        {
          prop: "operate",
          label: "操作",
          slot: true,
          width: 140,
          fixed: "right",
        },
      ],
      pageParams: {
        total: 0,
      },
      rowData: {},
      detailId: "",
      dialogType: "",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      selectRow: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,

      //海关关区信息列表
      customsList: [],
      // 场所列表
      fieldOptions: []
    };
  },
  computed: {
    dictProp() {  //字典list，为避免子组件重复调接口获取，用computed计算传值
      return {
        type: this.dict.type,
      }
    }
  },
  created() {
    this.getList();
    this.loadCustomsList();
    this.getZcFieldInfo();
  },
  methods: {
    /** 查询互市企业信息备案列表 */
    getList() {
      this.loading = true;
      listCorporationInfo(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.tableData = response.rows;
        this.pageParams.total = response.total;
        this.loading = false;
      });
    },
    /* 加载关区信息列表*/
    loadCustomsList() {
      getCustomsList().then((response) => {
        this.customsList = response;
      });
    },
    // 场所列表
    getZcFieldInfo() {
      let obj = {fieldName: null, customCode: null};
      findZcField(obj).then((response) => {
        this.fieldOptions = response;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs.tableList.pageParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val.pageSize;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val.currentPage;
      this.getList();
    },
    // 新增/编辑
    handleAddEdit(type, row) {
      this.dialogType = type;
      if (row) {
        this.rowData = JSON.parse(JSON.stringify(row));
      }
      if(this.ids.length === 1) {
        this.rowData = JSON.parse(JSON.stringify(this.selectRow));
      }
      this.$refs.addEdit.open();
    },
    //申报
    handleDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要申报的备案数据！");
        return false;
      }
      this.$modal.confirm('是否确认申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return declCorporationInfo(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    // 详情
    handleDetail(row) {
      this.detailId = row.id;
      this.$nextTick(() => {
        this.$refs.detail.open();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      if(selection.length === 1) {
        this.selectRow = selection[0];
      } else {
        this.selectRow = {};
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除互市企业信息备案编号为"' + ids + '"的数据项？').then(function () {
        return delCorporationInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有互市企业信息备案数据项？').then(() => {
        this.exportLoading = true;
        return exportCorporationInfo(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>


<style type="text/css">
.el-form-item .el-select {
  width: 100%;
}
</style>
